Package

Source Code of MineSweepSolver

import Block.*;

import java.awt.*;
import java.awt.event.InputEvent;
import java.util.ArrayList;
import java.util.Random;

/**
* Created with IntelliJ IDEA.
* User: Bram
* Date: 12-6-13
* Time: 15:13
* To change this template use File | Settings | File Templates.
*/
public class MineSweepSolver implements Runnable {
    private MineSweeper mineSweeper;
    private MineSweeperViewer mineSweeperViewer;
    int amountOfBlocksWidth = 10;
    int amountOfBlocksHeight = 10;

    private Block[][] blocks;
    private Random random;

    public MineSweepSolver() {

        try {
            mineSweeper = new MineSweeper(amountOfBlocksWidth, amountOfBlocksHeight);
        } catch (MineSweeperNotVisibleException e) {
            System.out.println("Cannot execute if minesweeper is not visible.\nProgram will now exit.");
            System.exit(0);
        } catch (AWTException e) {
            System.out.println("AWT Error.\nProgram will now exit.");
            System.exit(0);
        }
        blocks = mineSweeper.getBlocks();
        mineSweeperViewer = new MineSweeperViewer(mineSweeper);

        mineSweeper.update();
        //attemptToFlagMines();

        random = new Random();
        clickRandomMine();
        clickRandomMine();
        new Thread(this).start();
    }

    public static void main(String[] args){
        new MineSweepSolver();
    }

    @Override
    public void run() {
            while (true){
                mineSweeper.update();
                mineSweeperViewer.validate();
                mineSweeperViewer.repaint();


                if(mineSweeper.isInProgress()){
                    attemptToFlagMines();
                    attemptToGetNextMove();

                }
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) { e.printStackTrace(); }
            }
    }

    private ArrayList<Block> getBlocksAroundBlockByState(int column, int row, BlockState blockState){
        ArrayList<Block> blocksWithBlockState = new ArrayList<Block>();
        for(Direction direction : Direction.values()){
            Block block = getBlockFromDirection(column, row, direction);
            if(block!=null && block.getBlockState()!=null && block.getBlockState()== blockState)
                blocksWithBlockState.add(block);
        }
        return blocksWithBlockState;
    }

    private Block getBlockFromDirection(int column, int row, Direction direction){
        switch (direction){
            case NORTH:         column++;
                break;
            case NORTH_EAST:    column++;
                                row++;
                break;
            case EAST:          row++;
                break;
            case SOUTH_EAST:    column--;
                                row++;
                break;
            case SOUTH:         column--;
                break;
            case SOUTH_WEST:    column--;
                                row--;
                break;
            case WEST:          row--;
                break;
            case NORTH_WEST:    column++;
                                row--;
        }
        try{
            return blocks[column][row];
        }catch (ArrayIndexOutOfBoundsException e){return null;}
    }

    private enum Direction{
        NORTH, NORTH_EAST, EAST, SOUTH_EAST, SOUTH, SOUTH_WEST, WEST, NORTH_WEST;
    }

    private void clickRandomMine(){
        mineSweeper.setInProgress(true);
        mineSweeper.clickBlock(random.nextInt(amountOfBlocksWidth), random.nextInt(amountOfBlocksHeight), InputEvent.BUTTON1_MASK);
    }

    private void attemptToFlagMines(){
        for(int c=0; c<blocks.length; c++)
            for(int r=0; r<blocks[0].length; r++)
                if(blocks[c][r].getNumber()!=0){
                    ArrayList<Block> uncheckedBlocks = getBlocksAroundBlockByState(c,r, BlockState.UNCHECKED);
                    if(uncheckedBlocks.size()==blocks[c][r].getNumber()-getBlocksAroundBlockByState(c,r,BlockState.FLAG).size())
                        for(Block block : uncheckedBlocks)
                            if(block.getBlockState()!=BlockState.FLAG)
                                mineSweeper.clickBlock(block, InputEvent.BUTTON3_MASK);
                }

    }

    private void attemptToGetNextMove(){
        for(int c=0; c<blocks.length; c++)
            for(int r=0; r<blocks[0].length; r++)
                if(blocks[c][r].getNumber()!=0){
                    int blockNumber = blocks[c][r].getNumber();
                    ArrayList<Block> minesAroundNumber = getBlocksAroundBlockByState(c,r,BlockState.FLAG);
                    ArrayList<Block> freeBlocksAroundNumber = getBlocksAroundBlockByState(c,r,BlockState.UNCHECKED);

                    if(blockNumber == minesAroundNumber.size() & !freeBlocksAroundNumber.isEmpty()){
                        //if(freeBlocksAroundNumber.size()-minesAroundNumber.size())
                        //for(Block block : freeBlocksAroundNumber)
                        //    mineSweeper.clickBlock(block, InputEvent.BUTTON1_MASK);
                        mineSweeper.clickBlock(c,r,InputEvent.BUTTON2_MASK);

                    }

                }
    }




}
TOP

Related Classes of MineSweepSolver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.